---
title: 'Both: Deciles'
author: 'David Hume'
date: '`r format(Sys.time(), "%d %B %Y")`'
output: 
   html_document:
    toc: true
    toc_depth: 3 
editor_options: 
  chunk_output_type: console
---
---

```{r setup, message=FALSE, include = FALSE}
library(tidyverse)
library(data.table)
library(Hmisc)
library(fasttime)
library(lubridate)
library(stringr)
library(cowplot)
library(car)
library(stargazer)
library(lfe)
library(plm)
library(knitr)
library(pryr)
library(DescTools)
library(broom)

options(width=200, scipen=10)
knitr::opts_chunk$set(echo = TRUE, cache = FALSE, warning = FALSE, 
                      message = FALSE, cache.lazy = FALSE)

```

<div style="line-height: 2em;">
<font size="4"> 

---

```{r echo = FALSE, eval=TRUE}

Time <- Sys.time()

# Set-up

# Switches & setting of variables

# Data set either "All8pc", " "All1pc", "Test", "Render"
analysis.1 <- "Render"
# Filetype for output
# "latex" 
# "html" - inserting into html [htmltools::includeHTML()] or Word docs [Insert/[Text]/Object/Text from file]
# "text" - viewing on screen
filetype.1 <- "Render"


analysis <- ifelse(analysis.1 =="Render", readRDS("./Data/Analysis.rds"), analysis.1)
filetype <- ifelse(filetype.1 =="Render", readRDS("./Data/Filetype.rds"), filetype.1)


if (filetype=="latex") {
  out <- "tex"

  } else if (filetype=="html") {
      out <- "html"

    } else if (filetype=="text") {
      out <- "csv"

      } else {
        stop("latex, html or text")
}



# Set variables
fig = 0 # Counter for figures

```

```{r cache=FALSE, eval=TRUE, echo=FALSE}

Keep_cols <- c("Property_Id", "Type", "New", "Quality100000", "Region", "date.val", "YrQtr", "sold.dummy", "IdxPriceA", "PriceA", "DurationVal", "Peak_price", "DurationPeak", "Transactions_band", "Stock_band", "LTV_band")




#if (analysis.1!="Render") {
  if (analysis=="All") {
    f <- function(x, pos) subset(x, Type == Type) # i.e. reads in all data because condition always TRUE
      Resold.indexed <-  read_csv_chunked("./Data/Resold.indexed.All.csv", DataFrameCallback$new(f), chunk_size = 2000000)
      Resold.indexed <- as.data.table(Resold.indexed)
      Resold.indexed.regress <- Resold.indexed[, ..Keep_cols]
      Resold.indexed <- NULL
      
      } else {
        Resold.indexed.regress <-fread(paste0("./Data/Resold.indexed.", analysis, ".csv"), select = Keep_cols)
  }
#}



Resold.indexed.regress <- Resold.indexed.regress[,`:=`(Type=factor(Type), 
                                                         New=factor(New), 
                                                         YrQtr=factor(YrQtr),
                                                         Region=factor(Region),
                                                         Property_Id = factor(Property_Id),
                                                         date.val=as.Date(date.val, origin ="1970-01-01"),
                                                       Val_year=factor(year(date.val)),
                                                         Val_qtr=factor(quarter(date.val)))][
                                                           ,`:=`(Type = relevel(Type, "Flat"), New = relevel(New, "Old"))]



cat("DATA:", analysis)

```







```{r cache=FALSE,  echo= FALSE}



regression_variables <- function(dt, Ref_price, Ref_duration, return) {
  # NB Ref_price needs quotes
  DT <- dt[, .(Ref_price=get(Ref_price), Duration = get(Ref_duration), IdxPriceA, PriceA, Purch_duration = DurationVal, 
               sold.dummy, Property_Id, YrQtr, Val_year, Val_qtr, Type, New, Quality100000, Region, GainPurch, GainPeak, Return_decile)]
  
  DT[, F_unrealgain :=round(IdxPriceA-Ref_price) # Calculates unrealised gain or loss - price paid vs index
     ][, `:=`(F_unrealgainAdj = Winsorize(F_unrealgain/100000, probs = c(0.01, 0.99), na.rm = TRUE),
              F_unrealgainYN = F_unrealgain >=0,
              Price_unrealgainAdj = Winsorize((IdxPriceA-PriceA)/100000, probs = c(0.01, 0.99), na.rm = TRUE),
              Price_unrealgainYN = ifelse(IdxPriceA-PriceA>=0, TRUE, FALSE))
       ][, `:=`(Purch_duration2 = Purch_duration^2,
                         Purch_duration3 = Purch_duration^3,
                         Purch_duration4 = Purch_duration^4,
                         Purch_duration5 = Purch_duration^5)]
  
  
  
  if (return=="P") {
   DT[, `:=`(F_unrealgain.pos = ifelse(F_unrealgainYN,10000000*F_unrealgainAdj/Ref_price,0),
                       F_unrealgain.neg = ifelse(!F_unrealgainYN,10000000*F_unrealgainAdj/Ref_price,0),
                       Price_unrealgain.pos = ifelse(Price_unrealgainYN,10000000*Price_unrealgainAdj/PriceA,0),
                       Price_unrealgain.neg = ifelse(!Price_unrealgainYN,10000000*Price_unrealgainAdj/PriceA,0))]
  
    } else if (return=="L") {
        DT[, `:=`(F_unrealgain.pos = ifelse(F_unrealgainYN,F_unrealgainAdj,0),
                       F_unrealgain.neg = ifelse(!F_unrealgainYN,F_unrealgainAdj,0),
                       Price_unrealgain.pos = ifelse(Price_unrealgainYN,Price_unrealgainAdj,0),
                       Price_unrealgain.neg = ifelse(!Price_unrealgainYN,Price_unrealgainAdj,0))]
    } else if (return=="NA") {
        DT<-DT
      } else {
        stop("L, P or NA")
        }

  
  DT  <- DT[, c("Ref_price", "IdxPriceA", "PriceA", "Price_unrealgainAdj", "F_unrealgainAdj", "F_unrealgain"):= NULL]


  }


DP4 <- function(x){format(round(x, 4),nsmall= 4)[1]}

BR4 <- function(x){paste0("(",DP4(x), ")")}
  
Dec_table <- function(object, sample){
    
    result <- data.table(tidy(object)) # requires library broom
    result <- result[, stars:= ifelse(p.value<.01, "***", ifelse(p.value<.05, "**", ifelse(p.value<.1, "*", "")))]
    Dec_summary <- fread(paste0("./Tables/Dec_summary_raw.", analysis, ".csv"))
    
    
    Dec_summary <- rbind(Dec_summary, data.table(Description=sample,Purchase_coeff=paste0(DP4(result[term=="Price_unrealgainYNTRUE",.(estimate)]),result[term=="Price_unrealgainYNTRUE",.(stars)]),
                                               Purchase_se=BR4(result[term=="Price_unrealgainYNTRUE",.(std.error)]), 
                                               Peak_coeff=paste0(DP4(result[term=="F_unrealgainYNTRUE",.(estimate)]),result[term=="F_unrealgainYNTRUE",.(stars)]), 
                                               Peak_se=BR4(result[term=="F_unrealgainYNTRUE",.(std.error)]),  
                                               Constant_coeff=paste0(DP4(result[term=="(Intercept)",.(estimate)]),result[term=="(Intercept)",.(stars)]), 
                                               Constant_se=BR4(result[term=="(Intercept)",.(std.error)])))
    
    fwrite(Dec_summary, paste0("./Tables/Dec_summary_raw.", analysis, ".csv"))
  }


DecObs_table <- function(DT, sample){
  DecObs_summary <- fread(paste0("./Tables/DecObs_summary_raw.", analysis, ".csv"))
  DecObs_summary <- rbind(DecObs_summary, data.table(Description=sample, 
                                               Gain_purchase=DT[GainPurch>0, .N],  
                                               Loss_purchase=DT[GainPurch<0, .N], 
                                               Gain_peak=DT[GainPeak>0, .N], 
                                               Loss_peak=DT[GainPeak<0, .N]))
  
  fwrite(DecObs_summary, paste0("./Tables/DecObs_summary_raw.", analysis, ".csv"))
}



Both_ols1 <- function(dt, Ref_price, Ref_duration, title, sample, return){
  
  # title e.g. "Purchase", "Peak", "Neighbour"
  # sample e.g. "Incl3yr", "Excl3yr"
  # format short (excl property characteristics) or long
  return <-"NA"
  
  DT <- regression_variables(dt, Ref_price, Ref_duration, return)


ols_m4 <- felm(sold.dummy ~ Price_unrealgainYN + F_unrealgainYN + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 + Type + New + Quality100000 + Region| 0|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)

DT  <- DT[, c("Purch_duration","Purch_duration2", "Purch_duration3", "Purch_duration4", "Purch_duration5") := NULL
          ][, c("Type", "New", "Quality100000", "Region"):= NULL]



ols_m3 <- felm(sold.dummy ~ Price_unrealgainYN + F_unrealgainYN |  0|0|  Property_Id + YrQtr, data =  DT, na.action=na.exclude)

ols_m1 <- felm(sold.dummy ~ Price_unrealgainYN |  0|0|  Property_Id + YrQtr, data =  DT, na.action=na.exclude)

ols_m2 <- felm(sold.dummy ~ F_unrealgainYN |  0|0|  Property_Id + YrQtr, data =  DT, na.action=na.exclude)

Covariate_Labels <- c("Gain Since Purchase = 1", "Gain Since Peak = 1", "Years From Purchase", "Detached = 1", "Semi-detached = 1","Terraced = 1","New-build = 1", "Quality (£100,000)")


table <- stargazer(ols_m1, ols_m2, ols_m3, ols_m4, no.space=TRUE, align=TRUE, digits = 4, digits.extra = 0,  dep.var.caption  = "",
            dep.var.labels = "Sale", object.names=FALSE, model.numbers=FALSE,  model.names=FALSE, column.labels = c("(1)", "(2)", "(3)", "(4)"), 
              covariate.labels=Covariate_Labels,
            add.lines = list(c("Years From Purchase Quintics", "NO", "NO", "NO", "YES"), 
                             c( "Region", "NO", "NO", "NO", "YES")),
              omit.stat=c("ser","f","adj.rsq"), omit=c("Purch_duration2", "Purch_duration3", "Purch_duration4", "Purch_duration5", "Region"), 
              float = F, omit.table.layout = "n", column.sep.width = "-15pt", table.layout="-dc#-tas-",
              type = "latex")


table <- str_replace_all(table, "\\^", "")
table <- str_replace_all(table, "R\\$\\{2\\}\\$", "R$^{2}$")
write.table(table[11:(length(table)-2)], col.names = F, row.names = F, quote = FALSE, paste0("./Tables/LaTeX/", title, sample, analysis, return, "Clean_ols.tex"))

stargazer(ols_m1, ols_m2, ols_m3, ols_m4, no.space=TRUE, align=TRUE, digits = 4, digits.extra = 0,  dep.var.caption  = "",
            dep.var.labels = "Sale", object.names=FALSE, model.numbers=FALSE,  model.names=FALSE, column.labels = c("(1)", "(2)", "(3)", "(4)"), 
              covariate.labels=Covariate_Labels,
            add.lines = list(c("Years From Purchase Quintics", "NO", "NO", "NO", "YES"), 
                             c( "Region", "NO", "NO", "NO", "YES")),
              omit.stat=c("ser","f","adj.rsq"), omit=c("Purch_duration2", "Purch_duration3", "Purch_duration4", "Purch_duration5", "Region"), 
              font.size = "footnotesize", omit.table.layout = "n", column.sep.width = "-15pt", 
              type = filetype, out=paste0("./Tables/", title, sample, analysis, return, "_ols.", out))
  
}


Both_ols2 <- function(dt, Ref_price, Ref_duration, title, sample, return){
  
  # title e.g. "Purchase", "Peak", "Both"
  # sample e.g. Description if median spli must contain eith "high" or "low"
  # return whether calculated as alevel or %
  
  
  DT <- regression_variables(dt, Ref_price, Ref_duration, return)

ols_m3 <- felm(sold.dummy ~ Price_unrealgainYN + Price_unrealgain.pos + Price_unrealgain.neg + F_unrealgainYN + F_unrealgain.pos + F_unrealgain.neg  + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 + Type + New + Quality100000 + Region| 0|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)

ols_m2 <- felm(sold.dummy ~ F_unrealgainYN + F_unrealgain.pos + F_unrealgain.neg  + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 + Type + New + Quality100000 + Region| 0|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)

ols_m1 <- felm(sold.dummy ~ Price_unrealgainYN + Price_unrealgain.pos + Price_unrealgain.neg + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 + Type + New + Quality100000 + Region| 0|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)

 if (str_detect(sample, "high") | str_detect(sample, "low")){
   Dec_table(ols_m3, sample)
  DecObs_table(DT, sample)
   }
# Where applicable update median split summary table

 if (return=="P") {
Covariate_Labels <- c("Gain Since Purchase = 1", "Return Since Purchase > 0 (%)",  "Return Since Purchase < 0 (%)", "Gain Since Peak = 1", "Return Since Peak > 0 (%)",  "Return Since Peak < 0 (%)", "Years From Purchase")
  
    } else if (return=="L") {
Covariate_Labels <- c("Gain Since Purchase = 1", "Return Since Purchase > 0 (£100,000)",  "Return Since Purchase < 0 (£100,000)", "Gain Since Peak = 1", "Return Since Peak > 0 (£100,000)",  "Return Since Peak < 0 (£100,000)", "Years From Purchase")
} else {
        stop("L or P")
        }


table <- stargazer(ols_m1, ols_m2, ols_m3,  no.space=TRUE, align=TRUE, digits = 4, digits.extra = 0,  dep.var.caption  = "",
            dep.var.labels = "Sale", object.names=FALSE, model.numbers=FALSE,  model.names=FALSE, column.labels = c("(1)", "(2)", "(3)"), 
              covariate.labels=Covariate_Labels,
            add.lines = list(c("Years From Purchase Quintics", "YES", "YES", "YES"), 
                             c( "Property Characteristics", "YES", "YES", "YES")),
              omit.stat=c("ser","f","adj.rsq"), omit=c("Purch_duration2", "Purch_duration3", "Purch_duration4", "Purch_duration5", "Type", "New", "Quality100000", "Region"), 
              float = F, omit.table.layout = "n", column.sep.width = "-15pt", table.layout="-dc#-tas-",
              type = "latex")


table <- str_replace_all(table, "\\^", "")
table <- str_replace_all(table, "R\\$\\{2\\}\\$", "R$^{2}$")
write.table(table[11:(length(table)-2)], col.names = F, row.names = F, quote = FALSE, paste0("./Tables/LaTeX/", title, sample, analysis, return, "Clean_ols.tex"))
  
stargazer(ols_m1, ols_m2, ols_m3,  no.space=TRUE, align=TRUE, digits = 4, digits.extra = 0,  dep.var.caption  = "",
            dep.var.labels = "Sale", object.names=FALSE, model.numbers=FALSE,  model.names=FALSE, column.labels = c("(1)", "(2)", "(3)"), 
              covariate.labels=Covariate_Labels,
            add.lines = list(c("Years From Purchase Quintics", "YES", "YES", "YES"), 
                             c( "Property Characteristics", "YES", "YES", "YES")),
              omit.stat=c("ser","f","adj.rsq"), omit=c("Purch_duration2", "Purch_duration3", "Purch_duration4", "Purch_duration5", "Type", "New", "Quality100000", "Region"), 
              font.size = "footnotesize", omit.table.layout = "n", column.sep.width = "-15pt", 
              type = filetype, out=paste0("./Tables/", title, sample, analysis, return, "_ols.", out))
    

}



Both_fe <- function(dt, Ref_price, Ref_duration, title, sample, return){
  
  # title e.g. "Purchase", "Peak", "Neighbour"
  # sample e.g. "Incl3yr", "Excl3yr"
  # format short (excl property characteristics) or long
  
  
  DT <- regression_variables(dt, Ref_price, Ref_duration, return)
  
  DT  <- DT[, c("Type", "New", "Quality100000", "Region"):= NULL]

ols_m3 <- felm(sold.dummy ~ Price_unrealgainYN + Price_unrealgain.pos + Price_unrealgain.neg + F_unrealgainYN + F_unrealgain.pos + F_unrealgain.neg  + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 | Property_Id + YrQtr|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)

ols_m2 <- felm(sold.dummy ~ F_unrealgainYN + F_unrealgain.pos + F_unrealgain.neg  + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 | Property_Id + YrQtr|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)

ols_m1 <- felm(sold.dummy ~ Price_unrealgainYN + Price_unrealgain.pos + Price_unrealgain.neg + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 | Property_Id + YrQtr|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)


 if (return=="P") {
Covariate_Labels <- c("Gain Since Purchase = 1", "Return Since Purchase > 0 (%)",  "Return Since Purchase < 0 (%)", "Gain Since Peak = 1", "Return Since Peak > 0 (%)",  "Return Since Peak < 0 (%)", "Years From Purchase")
  
    } else if (return=="L") {
Covariate_Labels <- c("Gain Since Purchase = 1", "Return Since Purchase > 0 (£100,000)",  "Return Since Purchase < 0 (£100,000)", "Gain Since Peak = 1", "Return Since Peak > 0 (£100,000)",  "Return Since Peak < 0 (£100,000)", "Years From Purchase")
} else {
        stop("L or P")
        }

table <- stargazer(ols_m1, ols_m2, ols_m3,  no.space=TRUE, align=TRUE, digits = 4, digits.extra = 0,  dep.var.caption  = "",
            dep.var.labels = "Sale", object.names=FALSE, model.numbers=FALSE,  model.names=FALSE, column.labels = c("(1)", "(2)", "(3)"), 
              covariate.labels=Covariate_Labels,
            add.lines = list(c("Years From Purchase Quintics", "YES", "YES", "YES"), 
                             c( "Property FE", "YES", "YES", "YES"), 
                             c( "Quarter FE", "YES", "YES", "YES")),
              omit.stat=c("ser","f","adj.rsq"), omit=c("Purch_duration2", "Purch_duration3", "Purch_duration4", "Purch_duration5", "Type", "New", "Quality100000", "Region"), 
              float = F, omit.table.layout = "n", column.sep.width = "-15pt", table.layout="-dc#-tas-",
              type = "latex")


table <- str_replace_all(table, "\\^", "")
table <- str_replace_all(table, "R\\$\\{2\\}\\$", "R$^{2}$")
write.table(table[11:(length(table)-2)], col.names = F, row.names = F, quote = FALSE, paste0("./Tables/LaTeX/", title, sample, analysis, return, "Clean_fe.tex"))
  
stargazer(ols_m1, ols_m2, ols_m3,  no.space=TRUE, align=TRUE, digits = 4, digits.extra = 0,  dep.var.caption  = "",
            dep.var.labels = "Sale", object.names=FALSE, model.numbers=FALSE,  model.names=FALSE, column.labels = c("(1)", "(2)", "(3)"), 
              covariate.labels=Covariate_Labels,
            add.lines = list(c("Years From Purchase Quintics", "YES", "YES", "YES"), 
                             c( "Property FE", "YES", "YES", "YES"), 
                             c( "Quarter FE", "YES", "YES", "YES")),
              omit.stat=c("ser","f","adj.rsq"), omit=c("Purch_duration2", "Purch_duration3", "Purch_duration4", "Purch_duration5", "Type", "New", "Quality100000", "Region"), 
              font.size = "footnotesize", omit.table.layout = "n", column.sep.width = "-15pt", 
              type = filetype, out=paste0("./Tables/", title, sample, analysis, return, "_fe.", out))
    

}



Both_olsDecile<- function(dt, Ref_price, Ref_duration, title, sample, return){
  
  # title e.g. "Purchase", "Peak", "Both"
  # sample e.g. Description if median spli must contain eith "high" or "low"
  # return whether calculated as alevel or %
  
  
  DT <- regression_variables(dt, Ref_price, Ref_duration, return)

ols_m3 <- felm(sold.dummy ~ Price_unrealgainYN + Price_unrealgain.pos + Price_unrealgain.neg + F_unrealgainYN + F_unrealgain.pos + F_unrealgain.neg  + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 + Type + New + Quality100000 + Region| 0|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)

ols_m2 <- felm(sold.dummy ~ F_unrealgainYN + F_unrealgain.pos + F_unrealgain.neg  + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 + Type + New + Quality100000 + Region| 0|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)

ols_m1 <- felm(sold.dummy ~ Price_unrealgainYN + Price_unrealgain.pos + Price_unrealgain.neg + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 + Type + New + Quality100000 + Region| 0|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)

 if (return=="P") {
Covariate_Labels <- c("Gain Since Purchase = 1", "Return Since Purchase > 0 (%)",  "Return Since Purchase < 0 (%)", "Gain Since Peak = 1", "Return Since Peak > 0 (%)",  "Return Since Peak < 0 (%)", "Years From Purchase")
  
    } else if (return=="L") {
      
Dec_table(ols_m3, sample)
DecObs_table(DT, sample)
      
Covariate_Labels <- c("Gain Since Purchase = 1", "Return Since Purchase > 0 (£100,000)",  "Return Since Purchase < 0 (£100,000)", "Gain Since Peak = 1", "Return Since Peak > 0 (£100,000)",  "Return Since Peak < 0 (£100,000)", "Years From Purchase")
} else {
        stop("L or P")
        }


table <- stargazer(ols_m1, ols_m2, ols_m3,  no.space=TRUE, align=TRUE, digits = 4, digits.extra = 0,  dep.var.caption  = "",
            dep.var.labels = "Sale", object.names=FALSE, model.numbers=FALSE,  model.names=FALSE, column.labels = c("(1)", "(2)", "(3)"), 
              covariate.labels=Covariate_Labels,
            add.lines = list(c("Years From Purchase Quintics", "YES", "YES", "YES"), 
                             c( "Property Characteristics", "YES", "YES", "YES")),
              omit.stat=c("ser","f","adj.rsq"), omit=c("Purch_duration2", "Purch_duration3", "Purch_duration4", "Purch_duration5", "Type", "New", "Quality100000", "Region"), 
              float = F, omit.table.layout = "n", column.sep.width = "-15pt", table.layout="-dc#-tas-",
              type = "latex")


table <- str_replace_all(table, "\\^", "")
table <- str_replace_all(table, "R\\$\\{2\\}\\$", "R$^{2}$")
write.table(table[11:(length(table)-2)], col.names = F, row.names = F, quote = FALSE, paste0("./Tables/LaTeX/", title,"Dec" , analysis, return, "Clean_ols.tex"))
  
stargazer(ols_m1, ols_m2, ols_m3,  no.space=TRUE, align=TRUE, digits = 4, digits.extra = 0,  dep.var.caption  = "",
            dep.var.labels = "Sale", object.names=FALSE, model.numbers=FALSE,  model.names=FALSE, column.labels = c("(1)", "(2)", "(3)"), 
              covariate.labels=Covariate_Labels,
            add.lines = list(c("Years From Purchase Quintics", "YES", "YES", "YES"), 
                             c( "Property Characteristics", "YES", "YES", "YES")),
              omit.stat=c("ser","f","adj.rsq"), omit=c("Purch_duration2", "Purch_duration3", "Purch_duration4", "Purch_duration5", "Type", "New", "Quality100000", "Region"), 
              font.size = "footnotesize", omit.table.layout = "n", column.sep.width = "-15pt", 
              type = filetype, out=paste0("./Tables/", title,"Dec" ,sample, analysis, return, "_ols.", out))
    

}

DecOLS <- function(dt, Ref_price, Ref_duration, title, sample, return){
  DT <- regression_variables(dt[Return_decile==sample,], Ref_price, Ref_duration, return)
  
  DecObs_table(DT, sample)
  
  if (sample == 2) {
   ols <- felm(sold.dummy ~ Price_unrealgainYN + Price_unrealgain.pos + Price_unrealgain.neg + F_unrealgainYN + F_unrealgain.pos + F_unrealgain.neg  + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 + Type + New + Quality100000 + Region| 0|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)
} else {
   ols <- felm(sold.dummy ~ Price_unrealgain.pos + Price_unrealgain.neg + F_unrealgainYN + F_unrealgain.pos + F_unrealgain.neg  + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 + Type + New + Quality100000 + Region| 0|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)
}
  
  
 
  Dec_table(ols, sample)
  return(ols)
}




```



```{r cache=FALSE, eval=TRUE, echo=FALSE}

Resold.indexed.regress<-Resold.indexed.regress[!is.na(Peak_price),
                                               ][, `:=`(GainPurch = IdxPriceA - PriceA, GainPeak = IdxPriceA - Peak_price)]


#quintiles <- quantile(Resold.indexed.regress[,GainPurch], probs = seq(0, 1, 0.2), na.rm = TRUE)
deciles <- quantile(Resold.indexed.regress[,GainPurch], probs = seq(0, 1, 0.1), na.rm = TRUE)
deciles_tab <- data.table(rbind(0:10, deciles))


Resold.indexed.regress[,`:=`(Return_decile = cut(GainPurch, breaks = deciles, labels = FALSE, include.lowest = TRUE),
                             Return_decile_int = cut(GainPurch, breaks = deciles, include.lowest = TRUE))]

print(deciles)
print(tail(Resold.indexed.regress[,Return_decile_int]), 10)
fwrite(deciles_tab, paste0("./Tables/Deciles.", analysis, ".csv"))
```



***
# Deciles


```{r cache=FALSE, eval=TRUE, echo=FALSE}
Dec_summary <- data.table(Description="Decile", Purchase_coeff="X",  Purchase_se="X", Peak_coeff="X", Peak_se="X",  Constant_coeff="X", Constant_se="X")

fwrite(Dec_summary, paste0("./Tables/Dec_summary_raw.", analysis, ".csv"))


DecObs_summary <- data.table(Description="Decile", Gain_purchase="No.",  Loss_purchase="No.", Gain_peak="No.", Loss_peak="No.")
fwrite(DecObs_summary, paste0("./Tables/DecObs_summary_raw.", analysis, ".csv"))

all_regressions <- list()

for (Decile in 1:10) {
  all_regressions[[Decile]] <- DecOLS(Resold.indexed.regress, "Peak_price", "DurationPeak", "Both", Decile, "L")
  cat("Decile", Decile)
  print(summary(all_regressions[[Decile]]))
}

cat("Summary of results")
print(fread(paste0("./Tables/Dec_summary_raw.", analysis, ".csv")))
cat("Summary of observations")
print(fread(paste0("./Tables/DecObs_summary_raw.", analysis, ".csv")))

Covariate_Labels <- c("Gain Since Purchase = 1", "Return Since Purchase > 0 (£100,000)",  "Return Since Purchase < 0 (£100,000)", "Gain Since Peak = 1", "Return Since Peak > 0 (£100,000)",  "Return Since Peak < 0 (£100,000)", "Years From Purchase")

table <- stargazer(all_regressions[[1]],all_regressions[[2]],all_regressions[[3]],all_regressions[[4]],all_regressions[[5]],all_regressions[[6]],all_regressions[[7]],all_regressions[[8]],all_regressions[[9]],all_regressions[[10]], no.space=TRUE, align=TRUE, digits = 4, digits.extra = 0,  dep.var.caption  = "",
                   dep.var.labels = "Sale", object.names=FALSE, model.numbers=FALSE,  model.names=FALSE, column.labels = c("(1)", "(2)", "(3)", "(4)", "(5)", "(6)","(7)", "(8)", "(9)","(10)"), 
                   covariate.labels=Covariate_Labels,
                   add.lines = list(c("Years From Purchase Quintics", "YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES"), 
                                    c( "Property Characteristics", "YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES")),
                   omit.stat=c("ser","f","adj.rsq"), omit=c("Purch_duration2", "Purch_duration3", "Purch_duration4", "Purch_duration5", "Type", "New", "Quality100000", "Region"), 
                   float = F, omit.table.layout = "n", column.sep.width = "-15pt", table.layout="-dc#-tas-",
                   type = "latex")

table <- str_replace_all(table, "\\^", "")
table <- str_replace_all(table, "R\\$\\{2\\}\\$", "R$^{2}$")
write.table(table[11:(length(table)-2)], col.names = F, row.names = F, quote = FALSE, paste0("./Tables/LaTeX/", "Both","Dec" , analysis, "L", "Clean_ols.tex"))


stargazer(all_regressions[[1]],all_regressions[[2]],all_regressions[[3]],all_regressions[[4]],all_regressions[[5]],all_regressions[[6]],all_regressions[[7]],all_regressions[[8]],all_regressions[[9]],all_regressions[[10]],  no.space=TRUE, align=TRUE, digits = 4, digits.extra = 0,  dep.var.caption  = "",
          dep.var.labels = "Sale", object.names=FALSE, model.numbers=FALSE,  model.names=FALSE, column.labels = c("(1)", "(2)", "(3)", "(4)", "(5)", "(6)","(7)", "(8)", "(9)","(10)"), 
          covariate.labels=Covariate_Labels,
          add.lines = list(c("Years From Purchase Quintics", "YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES"), 
                           c( "Property Characteristics", "YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES")),
          omit.stat=c("ser","f","adj.rsq"), omit=c("Purch_duration2", "Purch_duration3", "Purch_duration4", "Purch_duration5", "Type", "New", "Quality100000", "Region"), 
          font.size = "footnotesize", omit.table.layout = "n", column.sep.width = "-15pt", 
          type = filetype, out=paste0("./Tables/", "Both","Dec" ,analysis, "L", "_ols.", out))

```


***



```{r cache=FALSE, eval=FALSE, echo=FALSE}
# Decile dummy table 1

Both_olsDecile(Resold.indexed.regress, "Peak_price", "DurationPeak", "Both", "Deciles", "L")

```







```{r cache=FALSE, eval=TRUE, echo=FALSE}
Sys.time() - Time

```

